﻿using ServerApp.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YServer;

namespace ServerApp.Cache
{
    public class AccountCache
    {
        int accid = 0;
        /// <summary>
        /// 账号缓存字典
        /// </summary>
        public Dictionary<string, Account> accDic = new Dictionary<string, Account>();

        /// <summary>
        /// 在线缓存字典
        /// </summary>
        public Dictionary<YToken, string> onlineDic = new Dictionary<YToken, string>(); 

        /// <summary>
        /// 判断是否存在该账号
        /// </summary>
        /// <param name="name">账号名称</param>
        /// <returns></returns>
        public bool HasAccount(string name) {
            //初始化账号
            Init(name);

            //账号不存在，返回 false
            if (!accDic.ContainsKey(name)) { return false; }

            return true;
        }

        /// <summary>
        /// 创建新账号
        /// </summary>
        /// <param name="name"></param>
        /// <param name="passwd"></param>
        public void CreateAccount(string name, string passwd) {
            Account model = new Account(accid, name, passwd);
            accid++;
            // 写入数据到数据库

            //更新缓存字典
            accDic.Add(name, model);
        }

        /// <summary>
        /// 根据账号名称初始化账号缓存字典
        /// </summary>
        /// <param name="name"></param>
        void Init(string name) {
            //若字典中存在该账号，则表示已经初始化，返回
            if (accDic.ContainsKey(name)) { return; }

            //否则，访问数据库，若账号存在则加入到账号缓存字典

        }

        /// <summary>
        /// 上线
        /// </summary>
        /// <param name="token">连接对象</param>
        /// <param name="passwd"></param>
        /// <returns></returns>
        public void Online(YToken token,string name) {
            onlineDic.Add(token, name);
        }

        /// <summary>
        /// 判断账号是否已经登录
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsOnline(string name){
            if(onlineDic.ContainsValue(name)){ return true; }

            return false;
        }

        public void Offline(YToken token) {
            if(onlineDic.ContainsKey(token))
                onlineDic.Remove(token);
        }


        /// <summary>
        /// 用户验证，密码是否正确
        /// </summary>
        /// <param name="name"></param>
        /// <param name="passwd"></param>
        /// <returns></returns>
        public bool IsPasswd(string name,string passwd){
           if(accDic[name].passwd.Equals(passwd)){ return true; }
            return false;
        }
        
        /// <summary>
        /// 根据连接返回账号id
        /// </summary>
        /// <param name="token"></param>
        /// <returns>
        /// -10 没有对应账号
        /// </returns>
        public int GetAccIdByToken(YToken token) {
            //如果该连接已登录且账号缓存字典有该账号时
            if (onlineDic.ContainsKey(token)&&accDic.ContainsKey(onlineDic[token])) {
                return accDic[onlineDic[token]].id;
            }

            return -10;
        }

        /// <summary>
        /// 通过账号名称获取连接对象
        /// </summary>
        /// <returns></returns>
        //public YToken GetTokenByName() { }
        //public YToken GetTokenById() { }

    }
}
